package bootstrap.liftweb

import net.liftweb._
import common._
import common.Full
import db.{StandardDBVendor, DefaultConnectionIdentifier}
import http.Html5Properties
import http.Html5Properties
import http.NotFoundAsTemplate
import http.ParsePath
import http.{LiftRules, NotFoundAsTemplate, ParsePath}
import mapper.{DB, Schemifier}
import sitemap._
import sitemap.Loc.If
import sitemap.Loc.LocGroup
import sitemap.Loc.MenuCssClass
import util.{Props, NamedPF}
import sitemap.Loc.{Unless, Test, If, Hidden}


import net.liftweb._
import util._
import http._
import provider.HTTPRequest
import Loc._


class Boot{// extends Config with Loggable{
  def boot {

    LiftRules.htmlProperties.default.set((r: Req) => new Html5Properties(r.userAgent))
    LiftRules.early.append(makeUtf8)


    LiftRules.statelessDispatch.append(per.brant.template.rest.PonyRest)

    // where to search snippet
    LiftRules.addToPackages("per.brant.template")

    def sitemap(): SiteMap = SiteMap(
      Menu("首页")   / "angular"

    )


    // build sitemap
    LiftRules.setSiteMapFunc(() => sitemap())

    LiftRules.uriNotFound.prepend(NamedPF("404handler") {
      case (req, failure) => NotFoundAsTemplate(
        ParsePath(List("exceptions", "404"), "html", false, false))
    })

    // set character encoding
    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))


/*
    // jndi details
    DefaultConnectionIdentifier.jndiName =Props.get("jdbc_type").openOr("jdbc/local")
    // fall back to jdbc if jndi not available
    if (!DB.jndiJdbcConnAvailable_?) {
      println("Init database connect failure,user code default")
      DB.defineConnectionManager(DefaultConnectionIdentifier, DBVendor)
      LiftRules.unloadHooks.append(() =>DBVendor.closeAllConnections_!())
    }else{
      println("Init database connect successed!!!")
    }



    Schemifier.schemify(true, Schemifier.infoF _
    )

*/


    /*设置多语言*/
    LiftRules.resourceNames = "i18n/language" :: LiftRules.resourceNames

    /*取消唯一链接限制*/
    //SiteMap.enforceUniqueLinks = false

   
   // LiftRules.unloadHooks.append( () => {} )


 }
  private def makeUtf8(req: HTTPRequest): Unit = {
    req.setCharacterEncoding("UTF-8")
  }
}

trait Config {

  val logUrl = LiftRules.getResource("/props/test.default.logback.xml")
  logUrl.foreach(x => {
    Logger.setup = Full(Logback.withFile(x))
  })
}



object DBVendor extends StandardDBVendor(
  Props.get("db.driver").openOr("com.mysql.jdbc.Driver"),
  Props.get("db.url").openOr("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8"),
  Props.get("db.user") or Full("uertname"),
  Props.get("db.pass") or Full("password")
)
